{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Valid Anagram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isAnagram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有效的字母异位词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个字符串 <code><em>s</em></code> 和 <code><em>t</em></code> ，编写一个函数来判断 <code><em>t</em></code> 是否是 <code><em>s</em></code> 的字母异位词。</p>\n",
    "\n",
    "<p><strong>注意：</strong>若 <code><em>s</em></code> 和 <code><em>t</em></code><em> </em>中每个字符出现的次数都相同，则称 <code><em>s</em></code> 和 <code><em>t</em></code><em> </em>互为字母异位词。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> <em>s</em> = \"anagram\", <em>t</em> = \"nagaram\"\n",
    "<strong>输出:</strong> true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> <em>s</em> = \"rat\", <em>t</em> = \"car\"\n",
    "<strong>输出: </strong>false</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>s</code> 和 <code>t</code> 仅包含小写字母</li>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>进阶: </strong>如果输入字符串包含 unicode 字符怎么办？你能否调整你的解法来应对这种情况？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [valid-anagram](https://leetcode.cn/problems/valid-anagram/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [valid-anagram](https://leetcode.cn/problems/valid-anagram/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"anagram\"\\n\"nagaram\"', '\"rat\"\\n\"car\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        char_dict = {}\n",
    "        for c in s:\n",
    "            char_dict[c] = char_dict.setdefault(c,0)+1\n",
    "        for c in t:\n",
    "            if c not in char_dict:\n",
    "                return False\n",
    "            else:\n",
    "                if char_dict[c] == 0:\n",
    "                    return False\n",
    "                else:\n",
    "                    char_dict[c] = char_dict[c]-1\n",
    "        for k in char_dict:\n",
    "            if char_dict[k] != 0:\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        arrs, arrt = [0] * 26, [0] * 26\n",
    "        for c in s:\n",
    "            arrs[ord(c) - ord('a')] += 1\n",
    "        for c in t:\n",
    "            arrt[ord(c) - ord('a')] += 1\n",
    "        return arrs == arrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        while s != '':\n",
    "            if len(s) != len(t):\n",
    "                return False\n",
    "            tmp = s[0]\n",
    "            s = s.replace(tmp, '')\n",
    "            try:\n",
    "                t = t.replace(tmp, '')\n",
    "            except:\n",
    "                return False\n",
    "\n",
    "        if t != '':\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        import collections\n",
    "        dic_s = collections.Counter(s)\n",
    "        dic_t = collections.Counter(t)\n",
    "        if dic_s == dic_t:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1, dic2 = [0]*26, [0]*26\n",
    "        for item in s:\n",
    "            dic1[ord(item)-ord('a')] += 1\n",
    "        for item in t:\n",
    "            dic2[ord(item)-ord('a')] += 1\n",
    "        return dic1 == dic2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        Given two strings s and t , write a function to determine if t is an anagram of s.\n",
    "        \"\"\"\n",
    "        array_s = [0]*26\n",
    "        array_t = [0]*26\n",
    "\n",
    "        for letter in list(s):\n",
    "            array_s[ord(letter) - 97] += 1\n",
    "\n",
    "        for letter in list(t):\n",
    "            array_t[ord(letter) - 97] += 1\n",
    "\n",
    "        for i in range(26):\n",
    "            if array_t[i] != array_s[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        dict1 = {}\n",
    "        dict2 = {}\n",
    "\n",
    "        for i in s:\n",
    "            if i not in dict1:\n",
    "                dict1[i] = 1\n",
    "            else:\n",
    "                dict1[i] += 1\n",
    "\n",
    "        for i in t:\n",
    "            if i not in dict2:\n",
    "                dict2[i] = 1\n",
    "            else:\n",
    "                dict2[i] += 1\n",
    "\n",
    "        return dict2==dict1\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return ''.join(sorted(s)) == ''.join(sorted(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "        t = list(t)\n",
    "        s = list(s)\n",
    "        cnt = 0\n",
    "        flag = 0\n",
    "        if len(s) != len(t):\n",
    "            flag = 1\n",
    "        for x in t:\n",
    "            if x not in s:\n",
    "                flag = 1\n",
    "                break\n",
    "            else:\n",
    "                s.remove(x)\n",
    "        return flag == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "        cnt = 0\n",
    "        s = list(s)\n",
    "        t = list(t)\n",
    "        lengt = len(t)\n",
    "        lengs = len(s)\n",
    "        if lengs != lengt:\n",
    "            return bool(0)\n",
    "        while(1):\n",
    "            try:\n",
    "                i = t.pop()\n",
    "            except:\n",
    "                break\n",
    "            if i in s:\n",
    "                s.remove(i)\n",
    "                cnt += 1\n",
    "                # print(s, t)\n",
    "        return lengt == cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return sorted(s) == sorted(t)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if s == t:\n",
    "            return True\n",
    "        elif not all([s, t]):\n",
    "            return False\n",
    "        \n",
    "        s, t = list(s), list(t)\n",
    "        s.sort()\n",
    "        t.sort()\n",
    "        return s == t\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "        s = [i for i in s]\n",
    "        t = [j for j in t]\n",
    "        s.sort()\n",
    "        t.sort()\n",
    "        if s == t:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        '''a=[]\n",
    "        a=list(t)\n",
    "        for ss in s:\n",
    "            if ss in a:\n",
    "                a.remove(ss)\n",
    "        if(len(a)==0):\n",
    "            return True\n",
    "        else:\n",
    "            return False'''\n",
    "        a1=[]\n",
    "        a2=[]\n",
    "        a1=list(s)\n",
    "        a1.sort()\n",
    "        a2=list(t)\n",
    "        a2.sort()\n",
    "        if a1==a2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        s=list(s)\n",
    "        t=list(t)\n",
    "        s=sorted(s)\n",
    "        t=sorted(t)\n",
    "        if len(s)!=len(t):\n",
    "            return False \n",
    "        for i in range(0,len(s)):\n",
    "            if s[i]!=t[i]:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "            \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        def splitstr(x):\n",
    "            y=[]\n",
    "            for i in range(len(x)):\n",
    "                y.append(x[i])\n",
    "            return y\n",
    "        \n",
    "        s=splitstr(s)\n",
    "        t=splitstr(t)\n",
    "        s.sort()\n",
    "        t.sort()\n",
    "        if s==t:\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "        return sorted(list(s))==sorted(list(t))\n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        else:\n",
    "            s=list(s)\n",
    "            for i in t:\n",
    "                if i in s:\n",
    "                    s.remove(i)\n",
    "                else:\n",
    "                    return False\n",
    "            return True\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import operator\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        a = list(s)\n",
    "        b = list(t)\n",
    "        m = sorted(a)\n",
    "        n = sorted(b)\n",
    "\n",
    "        return operator.eq(m, n)\n",
    "\n",
    "s = 'acbd'\n",
    "t = 'abcd'\n",
    "c = Solution()\n",
    "c.isAnagram(s, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        slist=[]\n",
    "        tlist=[]\n",
    "        for n in s:\n",
    "            slist.append(n)\n",
    "        for m in t:\n",
    "            tlist.append(m)\n",
    "        slist=sorted(slist)\n",
    "        tlist=sorted(tlist)\n",
    "        if slist==tlist:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        slist=[]\n",
    "        tlist=[]\n",
    "        for n in s:\n",
    "            slist.append(n)\n",
    "        for m in t:\n",
    "            tlist.append(m)\n",
    "        slist=sorted(slist)\n",
    "        tlist=sorted(tlist)\n",
    "        if slist==tlist:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if s=='' and t=='':\n",
    "            return True\n",
    "        if len(s)==1 and len(t)==1:\n",
    "            if s == t:\n",
    "                return True\n",
    "        a,b=[],[]\n",
    "        for i in s:\n",
    "            a.append(i)\n",
    "        for j in t:\n",
    "            b.append(j)\n",
    "        A =sorted(a)\n",
    "        B =sorted(b)\n",
    "        if A == B:\n",
    "            if a != b:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: 'str', t: 'str') -> 'bool':\n",
    "        dictl = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':25,'j':9,'k':10,'l':11,'m':12,\n",
    "                'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':26}\n",
    "        l1 = []\n",
    "        l2 = []\n",
    "        \n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        else:\n",
    "            for i in range(len(s)):\n",
    "                l1.append(dictl[s[i]])\n",
    "                l2.append(dictl[t[i]])\n",
    "            print(sorted(l1),sorted(l2))\n",
    "            if sorted(l1)==sorted(l2):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        new_s = {}\n",
    "        new_t={}\n",
    "        for i in s:\n",
    "            if i not in new_s:\n",
    "                new_s[i]=1\n",
    "            else:\n",
    "                new_s[i]+=1\n",
    "        for j in t:\n",
    "            if j not in new_t:\n",
    "                new_t[j]=1\n",
    "            else:\n",
    "                new_t[j]+=1\n",
    "\n",
    "        if new_t == new_s:\n",
    "            res = True\n",
    "        else:\n",
    "            res = False\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        \n",
    "        \n",
    "        cache1={}\n",
    "        for c in s:\n",
    "            cache1[c]=cache1.get(c,0)+1\n",
    "        \n",
    "        for c in t:\n",
    "            if c not in cache1:\n",
    "                return False\n",
    "            \n",
    "            cache1[c]-=1\n",
    "        \n",
    "        for c,val in cache1.items():\n",
    "            if val!=0:\n",
    "                return False\n",
    "            \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        ls = len(s)\n",
    "        lt = len(t)\n",
    "        if ls != lt:\n",
    "            return False\n",
    "        stas = {}\n",
    "        stat = {}\n",
    "        for ch in s:\n",
    "            stas[ch] = stas.get(ch,0)+1\n",
    "        for ch in t:\n",
    "            stat[ch] = stat.get(ch,0) + 1\n",
    "            \n",
    "        if stas == stat:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        d1 = {}\n",
    "        d2 = {}\n",
    "        for i in s:\n",
    "            if i not in d1:\n",
    "                d1[i] = 1\n",
    "            else:\n",
    "                d1[i] += 1\n",
    "        for i in t:\n",
    "            if i not in d2:\n",
    "                d2[i] = 1\n",
    "            else:\n",
    "                d2[i] += 1\n",
    "        return d1 == d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        import collections\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        dic = collections.defaultdict(int)\n",
    "        for i in range(len(s)):\n",
    "            dic[s[i]] += 1\n",
    "            dic[t[i]] -= 1\n",
    "        for value in dic.values():\n",
    "            if value != 0:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        d1, d2 = {}, {}\n",
    "        for i in range(len(s)):\n",
    "            d1[s[i]] = d1[s[i]] + 1 if s[i] in d1 else 1\n",
    "            d2[t[i]] = d2[t[i]] + 1 if t[i] in d2 else 1\n",
    "        for i in d1:\n",
    "            if i not in d2 or d1[i] != d2[i]:\n",
    "                return False\n",
    "        for i in d2:\n",
    "            if i not in d1 or d2[i] != d1[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic = {}\n",
    "        for item in s:\n",
    "            if item not in dic.keys():\n",
    "                dic[item] = 1\n",
    "            else:\n",
    "                dic[item] += 1\n",
    "        count = 0\n",
    "        for i in t:\n",
    "            if i not in dic.keys():\n",
    "                return False\n",
    "            else:\n",
    "                dic[i] -= 1\n",
    "            if dic[i] == 0:\n",
    "                count += 1\n",
    "        if count == len(dic):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        sl = list(s)\n",
    "        for word in t:\n",
    "            try:\n",
    "                sl.remove(word)\n",
    "            except:\n",
    "                return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        sl= list(s)\n",
    "        tl=list(t)\n",
    "        sl.sort()\n",
    "        tl.sort()\n",
    "        if sl==tl:\n",
    "            return True \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return sorted(s)==sorted(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        sl = list(s)\n",
    "        tl = list(t)\n",
    "        sl.sort()\n",
    "        tl.sort()\n",
    "        for i in range(len(s)):\n",
    "            if sl[i] != tl[i]:\n",
    "                return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s_dict = {}\n",
    "        t_dict = {}\n",
    "        if len(s) != len(t) :\n",
    "            return False\n",
    "        for x in s :\n",
    "            s_dict[x] = s.count(x)\n",
    "        for y in t :\n",
    "            if y not in s_dict.keys() or s_dict[y] != t.count(y) :\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(s):\n",
    "            return False\n",
    "        record = [0] * 26\n",
    "        for i in s:\n",
    "            record[ord(i) - ord(\"a\")] += 1\n",
    "        for i in t:\n",
    "            record[ord(i) - ord(\"a\")] -= 1\n",
    "        for i in record:\n",
    "            if i != 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return Counter(s) == Counter(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dict1 = {}\n",
    "        dict2 = {}\n",
    "        for i in s:\n",
    "            if i not in dict1:\n",
    "                dict1[i] = 1\n",
    "            else:\n",
    "                dict1[i] += 1\n",
    "        for j in t:\n",
    "            if j not in dict2:\n",
    "                dict2[j] = 1\n",
    "            else:\n",
    "                dict2[j] += 1\n",
    "        if dict1 == dict2:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s1=set(list(s))\n",
    "        t1=set(list(t))\n",
    "        \n",
    "        if len(s1-t1)==0:\n",
    "            for i in (s1|t1):\n",
    "                a=s.count(i)\n",
    "                b=t.count(i)\n",
    "                if a!=b:\n",
    "                    return False\n",
    "            return True\n",
    "        else: \n",
    "            return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        char_table = [0 for _ in range(26)]\n",
    "        for char in s:\n",
    "            char_table[ord(char) - ord('a')] += 1\n",
    "        for char in t:\n",
    "            char_table[ord(char) - ord('a')] -= 1\n",
    "\n",
    "        if any(char_table):\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic=defaultdict(int)\n",
    "        for x in s:\n",
    "            dic[x]+=1\n",
    "        for y in t:\n",
    "            dic[y]-=1\n",
    "        for z in dic.values():\n",
    "            if z!=0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        dic = defaultdict(int)\n",
    "        for c in s:\n",
    "            dic[c] += 1\n",
    "        for c in t:\n",
    "            dic[c] -= 1\n",
    "        for val in dic.values():\n",
    "            if val != 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        return True if Counter(s) == Counter(t) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        dic1={}\n",
    "        dic2={}\n",
    "        if len(s) == len(t):\n",
    "            for i in s :\n",
    "                dic1[i] = dic1.get(i,0)+1\n",
    "            for i in t :\n",
    "                dic2[i] = dic2.get(i,0)+1\n",
    "            if dic1 == dic2:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s1 = list(s)\n",
    "        s1.sort()\n",
    "        t1 = list(t)\n",
    "        t1.sort()\n",
    "        #print(s1,\"11\",t1)\n",
    "        return s1 == t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isAnagram(self, s: str, t: str) -> bool:\n",
    "        s = sorted(s)\n",
    "        t = sorted(t)\n",
    "        return s == t"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
